home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / xlib / tri2.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  7KB  |  272 lines

  1. /*
  2.  * (c) Copyright 1993-94, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software for
  6.  * any purpose and without fee is hereby granted, provided that the above
  7.  * copyright notice appear in all copies and that both the copyright notice
  8.  * and this permission notice appear in supporting documentation, and that
  9.  * the name of Silicon Graphics, Inc. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.
  12.  *
  13.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  17.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  22.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
  27.  * Use, duplication, or disclosure by the Government is subject to
  28.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30.  * clause at DFARS 252.227-7013 and/or in similar or successor
  31.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  32.  * Unpublished-- rights reserved under the copyright laws of the
  33.  * United States.  Contractor/manufacturer is Silicon Graphics,
  34.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  35.  *
  36.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37.  */
  38. #include <GL/glx.h>
  39. #include <GL/glu.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <X11/keysym.h>
  43.  
  44. static int attributes[] = {
  45.     GLX_RGBA,
  46.     GLX_RED_SIZE, 1,
  47.     GLX_GREEN_SIZE, 1,
  48.     GLX_BLUE_SIZE, 1,
  49.     None,
  50. };
  51.  
  52. int width = 200, height = 200;
  53. GLUquadricObj *quadObj;            
  54.  
  55. static void DoDisplay(GLfloat tx, GLfloat ty, GLfloat rx, GLfloat rz)
  56. {
  57.     glMatrixMode(GL_PROJECTION);
  58.     glLoadIdentity();
  59.     glOrtho(-0.5, width - 0.5, -0.5, height - 0.5, 0.01, 1000.0);
  60.     glMatrixMode(GL_MODELVIEW);
  61.     glLoadIdentity();
  62.     glTranslatef(100 + tx, 100 + ty, -10);
  63.     glRotatef(rx, 1, 0, 0);
  64.     glRotatef(rz, 0, 0, 1);
  65.     glScalef(20, 20, 20);
  66.  
  67.     glViewport(0, 0, width, height);
  68.     glClearColor(0.25, 0.25, 0.25, 1.0);
  69.     glClear(GL_COLOR_BUFFER_BIT);
  70.     glShadeModel(GL_FLAT);
  71.  
  72.     /* Draw a bunch of triangles that converge on a single point */
  73.     glBegin(GL_TRIANGLE_FAN);
  74.     glColor3f(1, 0, 0);
  75.     glVertex2f(0, 0);
  76.     glVertex2f(-1, -0.5);
  77.     glVertex2f(-1, -0.25);
  78.     glColor3f(0, 1, 0);
  79.     glVertex2f(-1, 0);
  80.     glColor3f(0, 0, 1);
  81.     glVertex2f(-1, 0.25);
  82.     glColor3f(1, 1, 1);
  83.     glVertex2f(-1, 0.5);
  84.     glEnd();
  85.  
  86.     glBegin(GL_TRIANGLE_FAN);
  87.     glColor3f(1, 0, 0);
  88.     glVertex2f(0, 0);
  89.     glVertex2f(-0.5, 1);
  90.     glVertex2f(-0.25, 1);
  91.     glColor3f(0, 1, 0);
  92.     glVertex2f(0, 1);
  93.     glColor3f(0, 0, 1);
  94.     glVertex2f(0.25, 1);
  95.     glColor3f(1, 1, 1);
  96.     glVertex2f(0.5, 1);
  97.     glEnd();
  98.  
  99.     glBegin(GL_TRIANGLE_FAN);
  100.     glColor3f(1, 0, 0);
  101.     glVertex2f(0, 0);
  102.     glVertex2f(1, 0.5);
  103.     glVertex2f(1, 0.25);
  104.     glColor3f(0, 1, 0);
  105.     glVertex2f(1, 0);
  106.     glColor3f(0, 0, 1);
  107.     glVertex2f(1, -0.25);
  108.     glColor3f(1, 1, 1);
  109.     glVertex2f(1, -0.5);
  110.     glEnd();
  111.  
  112.     glBegin(GL_TRIANGLE_FAN);
  113.     glColor3f(1, 0, 0);
  114.     glVertex2f(0, 0);
  115.     glVertex2f(-0.5, -1);
  116.     glVertex2f(-0.25, -1);
  117.     glColor3f(0, 1, 0);
  118.     glVertex2f(0, -1);
  119.     glColor3f(0, 0, 1);
  120.     glVertex2f(0.25, -1);
  121.     glColor3f(1, 1, 1);
  122.     glVertex2f(0.5, -1);
  123.     glEnd();
  124.  
  125.     glPushMatrix();
  126.     gluQuadricDrawStyle (quadObj, GLU_FILL);
  127.     glColor3f (3.2, 0.8, 1.0);
  128.     gluDisk(quadObj,2.0,5.0,16,4);
  129.  
  130. }
  131.  
  132. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  133. {
  134.     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  135.     return GL_TRUE;
  136.     }
  137.     return GL_FALSE;
  138. }
  139.  
  140. int main()
  141. {
  142.     XVisualInfo *vi;
  143.     Display *dpy;
  144.     Colormap cmap;
  145.     Window window;
  146.     XSetWindowAttributes swa;
  147.     GLXContext cx;
  148.     XEvent event;
  149.     GLboolean needDisplay;
  150.     GLfloat tx = 0;
  151.     GLfloat ty = 0;
  152.     GLfloat rx = 0;
  153.     GLfloat rz = 0;
  154.  
  155.     dpy = XOpenDisplay(0);
  156.     if (!dpy) {
  157.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  158.     return -1;
  159.     }
  160.  
  161.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
  162.     if (!vi) {
  163.     fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
  164.         getenv("DISPLAY"));
  165.     return -1;
  166.     }
  167.  
  168.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  169.                AllocNone);
  170.     swa.border_pixel = 0;
  171.     swa.colormap = cmap;
  172.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  173.     | KeyReleaseMask;
  174.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  175.                width, height,
  176.                0, vi->depth, InputOutput, vi->visual,
  177.                CWBorderPixel|CWColormap|CWEventMask, &swa);
  178.     XSetWMColormapWindows(dpy, window, &window, 1);
  179.     XMapWindow(dpy, window);
  180.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  181.  
  182.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  183.     if (!glXMakeCurrent(dpy, window, cx)) {
  184.     fprintf(stderr, "Can't make window current to context\n");
  185.     return -1;
  186.     }
  187.  
  188.     quadObj = gluNewQuadric ();
  189.  
  190.     needDisplay = GL_TRUE;
  191.     for (;;) {
  192.     do {
  193.         XNextEvent(dpy, &event);
  194.         switch (event.type) {
  195.           case Expose:
  196.         needDisplay = GL_TRUE;
  197.         break;
  198.           case ConfigureNotify:
  199.         width = event.xconfigure.width;
  200.         height = event.xconfigure.height;
  201.         needDisplay = GL_TRUE;
  202.         break;
  203.           case KeyPress:
  204.         {
  205.             char buf[100];
  206.             int rv;
  207.             KeySym ks;
  208.  
  209.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  210.             switch (ks) {
  211.               case XK_p:
  212.               case XK_P:
  213.             glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
  214.             needDisplay = GL_TRUE;
  215.             break;
  216.               case XK_l:
  217.               case XK_L:
  218.             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  219.             needDisplay = GL_TRUE;
  220.             break;
  221.               case XK_f:
  222.               case XK_F:
  223.             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  224.             needDisplay = GL_TRUE;
  225.             break;
  226.               case XK_Left:
  227.             tx -= 0.1;
  228.             needDisplay = GL_TRUE;
  229.             break;
  230.               case XK_Right:
  231.             tx += 0.1;
  232.             needDisplay = GL_TRUE;
  233.             break;
  234.               case XK_Up:
  235.             ty += 0.1;
  236.             needDisplay = GL_TRUE;
  237.             break;
  238.               case XK_Down:
  239.             ty -= 0.1;
  240.             needDisplay = GL_TRUE;
  241.             break;
  242.               case XK_KP_Left:
  243.             rz += 1;
  244.             needDisplay = GL_TRUE;
  245.             break;
  246.               case XK_KP_Right:
  247.             rz -= 1;
  248.             needDisplay = GL_TRUE;
  249.             break;
  250.               case XK_KP_Up:
  251.             rx += 1;
  252.             needDisplay = GL_TRUE;
  253.             break;
  254.               case XK_KP_Down:
  255.             rx -= 1;
  256.             needDisplay = GL_TRUE;
  257.             break;
  258.               case XK_Escape:
  259.             return 0;
  260.             }
  261.         }
  262.         break;
  263.         }
  264.     } while (XPending(dpy) != 0);
  265.  
  266.     if (needDisplay) {
  267.         needDisplay = GL_FALSE;
  268.         DoDisplay(tx, ty, rx, rz);
  269.     }
  270.     }
  271. }
  272.